package com.itheima.reggie.controller;

import com.itheima.reggie.config.R;
import com.itheima.reggie.dao.ShoppingCartDao;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        List<ShoppingCart> shoppingCarts=shoppingCartService.findCart(userId);
        return R.success(shoppingCarts);
    }

    /**
     * localhost:8080/shoppingCart/add
     *
     */
    @PostMapping("/add")
    public R<ShoppingCart> addCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        shoppingCart=shoppingCartService.add(shoppingCart);
        return  R.success(shoppingCart);

    }
    /**
     * localhost:8080/shoppingCart/add
     *
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
       shoppingCartService.clean(userId);
        return  R.success("清除成功");

    }
}
